/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xmlProject.ultils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import sample.accounts.Product;
import sample.accounts.Products;

/**
 *
 * @author Admin
 */
public class XMLUltils {

    public static void MarshallingProductsInfo(Object obj, String xmlOutputPath) {
        try {
            JAXBContext jc = JAXBContext.newInstance(obj.getClass());
            Marshaller mar = jc.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(obj, new File(xmlOutputPath));
        } catch (JAXBException ex) {
            Logger.getLogger(XMLUltils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void UnmarshallingProductsInfo(String xmlFile) throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance("sample.accounts");
        Unmarshaller um = jc.createUnmarshaller();
        File f = new File(xmlFile);
        Products jax = (Products) um.unmarshal(f);
        List<Product> products = jax.getProduct();


    }

    public static void transformXMLtoHTML(int id, String xsltFilepath, String htmlFilepath) throws FileNotFoundException {
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            StreamSource xsltFile = new StreamSource(xsltFilepath);
            Transformer trans = tf.newTransformer(xsltFile);
            StreamSource xmlFile = DBUltils.getProductInfoByID(id);
            StreamResult htmlFile = new StreamResult(new FileOutputStream(htmlFilepath));
            trans.transform(xmlFile, htmlFile);
        } catch (TransformerException ex) {
            System.out.println(ex.getMessage());
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static Document makeDomProducts()
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        DOMImplementation impl = db.getDOMImplementation();
        Document doc = impl.createDocument(null, "products", null);
        return doc;
    }

    public static Document parseDOMFromFile(String xmlFileName) {
        Document doc = null;
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.isNamespaceAware();
            File xmlFile = new File(xmlFileName);
            doc = db.parse(xmlFile);
        } catch (SAXException se) {
            System.out.println("Error in SAXException: " + se.getMessage());
        } catch (IOException ioe) {
            System.out.println("Error in IOException: " + ioe.getMessage());
        } catch (ParserConfigurationException pce) {
            System.out.println("Error in ParserConfigurationException: " + pce.getMessage());
        }
        return doc;
    }
}
